
import { _decorator, Component, Node, Vec3, MeshRenderer } from 'cc';
import yyComponent from '../../Script/Common/yyComponent';
import { GlobalEnum } from '../../Script/GameSpecial/GlobalEnum';
import GameConfig from '../../Script/GameSpecial/GameConfig';
import { EventType } from '../../Script/GameSpecial/GameEventType';
import { Role } from './Role';
const { ccclass, property } = _decorator;

@ccclass('Player')
export class Player extends Role {

    /************************************************************通用流程************************************************************/

    protected onEvents() {
        this.on(EventType.LevelEvent.removeBarrierGroupIndex, this.onRemoveBarrierGroupIndex, this);
        this.on(EventType.LevelEvent.startJoy, this.onStartJoy, this);
        this.on(EventType.LevelEvent.moveJoy, this.onMoveJoy, this);
        this.on(EventType.LevelEvent.stopJoy, this.onStopJoy, this);
    }

    /************************************************************对外功能************************************************************/
    public enterScoreRoad() {
        // console.error("玩家加零件");
        // this.car.addPartCount(100);
        this.node.active = true;
        this.hideHuoYan();
        this.updateSpeed();
        this.enterCustomUpdateState(GlobalEnum.RoleState.moveOnScoreRoad);
        this.off(EventType.LevelEvent.startJoy, this.onStartJoy, this);
        this.off(EventType.LevelEvent.moveJoy, this.onMoveJoy, this);
        this.off(EventType.LevelEvent.stopJoy, this.onStopJoy, this);
        this.on(EventType.LevelEvent.startJoy, this.onTouchDown, this);
        this.touchTime = 0.2;
        this.adTouched = false;
    }
    public enterEndRoadLose() {
        this.setTargetRadianZ(this.radianZ);
        this.carLayer.setRotation(0, 0, 0, 1);
        this.enterCustomUpdateState(GlobalEnum.RoleState.endRoad);
    }

    /************************************************************管理数据************************************************************/
    protected _isPlayer: boolean = true;

    /************************************************************管理对象************************************************************/

    /************************************************************运行状态************************************************************/

    /************************************************************事件回调************************************************************/
    protected onStartJoy() {
        this.startRadianZ = this.radianZ;
        //todo:测试用，按下手指开始移动
        // this.enterCustomUpdateState(GlobalEnum.RoleState.move);
    }
    protected onMoveJoy(x: number) {
        this.setTargetRadianZ(this.startRadianZ + x * 0.0016);
    }
    protected onStopJoy() {
        //todo:测试用，松开手指停止移动
        // this.enterCustomUpdateState(GlobalEnum.RoleState.stand);
    }

    //结尾路段连续点击事件
    protected touchTime: number = 0;
    protected adTouched: boolean = false;
    protected onTouchDown() {
        this.touchTime = 0.2;
        this.updateSpeed();
    }

    //终点路段，边跑边掉落零件
    protected stepMoveOnScoreRoad(dt: number) {
        //掉落零件
        this.elapsedOnScoreRoad += dt;
        if (this.elapsedOnScoreRoad > 0.4) {
            this.elapsedOnScoreRoad = 0;
            if (this.curPartCount > 0) {
                let count = Math.floor(this.car.getTotalPartCount() / this.car.partGroupCount);
                this.car.dropPartCount(count);
                this.curPartCount = this.car.getPartCount();
                this.emit(EventType.LevelEvent.updatePlayerCurPartCount, this.curPartCount);
                // this.updateSpeed();
            }
        }
        this.cachePos.z += this.speed.z * dt;
        this.touchTime -= dt;
        if (this.touchTime < 0) {
            // this.speed.z *= 0.98;
            this.speed.z += dt * 20;
            if (this.speed.z > 0) {
                this.speed.z = 0;
            }
        }
        this.node.setPosition(this.cachePos);
        this.updateCollScope();
        this.updateStreaks(dt);
    }

}
